<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
  <title>聊天室</title>
  <link rel="stylesheet" href="style.css">
  <style>
    html, body {
      height: 100%;
      margin: 0;
      padding: 0;
      background: #181818;
      color: #00ff00;
      font-family: monospace;
      width: 100vw;
      min-height: 100vh;
    }
    body {
      display: flex;
      align-items: center;
      justify-content: center;
      height: 100vh;
      width: 100vw;
    }
    .container {
      width: 100%;
      min-height: 100%;
      max-width: none;
      background: #222;
      box-shadow: 0 0 32px #00ff0033;
      border-radius: 0;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: flex-start;
      padding: 0 8px 72px; /* leave space for fixed input on mobile */
    }
    h1 {
      color: #00ff00;
      text-align: center;
      margin-top: 48px;
      font-size: 2.5rem;
      letter-spacing: 2px;
      text-shadow: 0 0 8px #00ff00;
    }
    #messages {
      background: #111;
      border: 1px solid #00ff00;
      flex: 1;
      width: 100%;
      max-width: 800px;
      min-height: 320px;
      max-height: 60vh;
      overflow-y: auto;
      padding: 16px;
      margin-bottom: 24px;
      font-size: 16px;
      border-radius: 8px;
      box-shadow: 0 0 8px #00ff0033;
  /* Hidden by default to avoid visible flash when not yet authenticated */
  display: none;
    }
  /* Minimal Geek-style messages: keep simple text, left for others, right for self */
  #messages { display:flex; flex-direction:column; }
  .message { margin-bottom: 8px; word-break: break-word; display:flex; flex-direction:column; }
  /* Each side uses at most half the messages area; align meta+bubble per side */
  .message.other { align-items: flex-start; }
  .message.me { align-items: flex-end; }
  .message .meta {
    font-size: 0.75rem;
    color: #99ffaa;
    margin-bottom: 6px;
    opacity: 0.95;
    letter-spacing: 0.6px;
  }
  
  .message.other .meta { text-align: left; margin-right: 0; }
  .message.me .meta { text-align: right; margin-left: 0; }
  .message .bubble {
    font-family: monospace;
    color: #00ff00;
    background: rgba(0,255,0,0.04);
    padding: 8px 12px;
    border-radius: 8px;
    max-width: 50%;
    box-sizing: border-box;
    white-space: normal;
    word-break: break-word;
    line-height: 1.3;
  }
    #messageForm {
      display: flex;
      width: 100%;
      max-width: 800px;
      margin-bottom: 32px;
  /* Hidden by default until user is authenticated */
  display: none;
    }
    #messageInput {
      flex: 1;
      background: #181818;
      color: #00ff00;
      border: 2px solid #00ff00;
      padding: 12px;
      font-size: 16px;
      border-radius: 8px 0 0 8px;
      outline: none;
      transition: border 0.2s;
    }
    #messageInput:focus {
      border-color: #00cc00;
    }
    button {
      background: linear-gradient(90deg, #00ff00 0%, #00cc00 100%);
      color: #181818;
      border: none;
      padding: 12px 32px;
      font-weight: bold;
      font-size: 16px;
      cursor: pointer;
      border-radius: 0 8px 8px 0;
      margin-left: 0;
      box-shadow: 0 0 8px #00ff0033;
      transition: background 0.2s, color 0.2s;
    }
    button:hover {
      background: linear-gradient(90deg, #00cc00 0%, #00ff00 100%);
      color: #fff;
    }
    .account {
      width: 80vw;
      max-width: 800px;
      text-align: right;
      margin-bottom: 8px;
      font-size: 1.1rem;
      margin-top: 8px;
    }
    .account a, .account span {
      color: #00ff00;
      font-weight: bold;
      text-decoration: none;
      margin-left: 16px;
      font-size: 1.1rem;
      letter-spacing: 1px;
    }
    .account a:hover {
      color: #00cc00;
      text-decoration: underline;
    }
    @media (max-width: 600px) {
      #messages, #messageForm, .account {
        width: 98%;
        max-width: none;
      }
      h1 { font-size: 1.5rem; }
    }
    /* Mobile improvements: pin input, use remaining height for messages, larger touch targets */
    @media (max-width: 700px) {
      html, body { height: 100%; }
      .container {
        width: 100vw;
        height: 100vh;
        padding: 12px 8px 72px; /* leave space for fixed input */
        justify-content: flex-start;
      }
      h1 { margin-top: 18px; font-size: 1.4rem; }
      #messages {
        width: 96vw;
        max-width: none;
        min-height: calc(100vh - 220px);
        max-height: calc(100vh - 220px);
        padding: 12px;
      }
  /* Slightly larger bubbles on mobile for readability, keep geek aesthetic */
  .message .bubble { max-width: 70%; padding: 10px 14px; }
  .message .meta { font-size: 0.7rem; }
      #messageForm {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        display: flex;
        justify-content: center;
        padding: 10px;
        background: linear-gradient(180deg, rgba(0,0,0,0.0), rgba(0,0,0,0.06));
      }
      #messageForm > * { max-width: 96vw; }
      #messageInput { font-size: 18px; padding: 14px; border-radius: 8px 0 0 8px; }
      button { padding: 14px 18px; font-size: 16px; border-radius: 0 8px 8px 0; }
      .account { width: 96vw; }
      #onlinePanel { width: 96vw; overflow-x: auto; white-space: nowrap; }
      #onlineUsers { display: inline-block; }
    }
  </style>
</head>
<body>
  <div class="container">
  <h1>&lt;聊天室/&gt;</h1>
    <div class="account" id="accountBar">
      <!-- Account info will be injected by JS -->
    </div>
    <div id="onlinePanel" style="width:80vw;max-width:800px;margin-bottom:12px;">
      <strong style="color:#00ff00;">在线用户：</strong>
      <div id="onlineUsers" style="display:inline-block;margin-left:8px;color:#00ff00;"></div>
    </div>
    <div id="messages"></div>
    <form id="messageForm">
      <input type="text" id="messageInput" placeholder="输入消息..." required autocomplete="off">
      <button type="submit">发送</button>
    </form>
    <input type="hidden" id="username" value="">
  </div>
    <script>
    // Set username from server session and update account bar (username links to /config)
    fetch('/whoami').then(r => r.json()).then(data => {
      document.getElementById('username').value = data.username;
      const accountBar = document.getElementById('accountBar');
      if (data.username) {
        accountBar.innerHTML = `<a href='/config' style='color:#00ff00;font-weight:bold;text-decoration:none'>${data.username}</a> <form action='/logout' method='POST' style='display:inline;margin-left:8px'><button type='submit'>退出</button></form>`;
      } else {
        accountBar.innerHTML = `<a href='/login'>登录</a> | <a href='/register'>注册</a>`;
      }
    });
  </script>
  <script src="/socket.io/socket.io.js"></script>
  <script src="client.js"></script>
</body>
</html>
